package com.bgs.travelapp.menu.service.impl;

import com.bgs.travelapp.menu.entity.MenuEntity;
import com.bgs.travelapp.menu.mapper.MenuMapper;
import com.bgs.travelapp.menu.service.MenuService;
import com.bgs.travelapp.page.MyPageHelper;
import com.bgs.travelapp.page.PageRequest;
import com.bgs.travelapp.page.PageResult;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单管理 （后端）
 * gpd
 * 2022-07-18 21点11分
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    MenuMapper menuMapper;

    /**
     * 遍历菜单 （后端---角色授权功能）
     * gpd 2022-07-18 09:16
     * 遍历出每个父级菜单
     * 放进Child方法里再遍历
     * gpd 2022-07-18 09:16
     * @return
     */
    @Override
    public List<MenuEntity> getMenuTree() {
        Example example = new Example(MenuEntity.class);
        example.createCriteria().andEqualTo("delFlag",0);
        List<MenuEntity> menuEntityList = menuMapper.selectByExample(example);
        List<MenuEntity> newMenuTree = new ArrayList<>();
        for(MenuEntity menuEntity :menuEntityList){
            if(menuEntity.getParentId()==0){
                newMenuTree.add(getChild(menuEntity,menuEntityList));
            }
        }
        return newMenuTree;
    }

    /**
     * 新增菜单 （后端---菜单管理）
     * gpd 2022-07-18 09:46
     * @param menuEntity
     * @return
     */
    @Override
    public Integer saveMenu(MenuEntity menuEntity) {
        menuEntity.setDelFlag(0);
        return menuMapper.insertSelective(menuEntity);
    }

    /**
     *全查 （后端---菜单管理）
     * gpd 2022-07-18 09:30
     * @return
     */
    @Override
    public PageResult getMenuAllListByPage(PageRequest pageRequest) {
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(MenuEntity.class);
        example.createCriteria().andEqualTo("delFlag",0);
        PageResult pageResult = MyPageHelper.pageInfo(menuMapper.selectByExample(example));
        return pageResult;
    }

    /**
     * 修改菜单 （后端---菜单管理）
     * gpd 2022-07-18 09:46
     * @param menuEntity
     * @return
     */
    @Override
    public Integer updateMenu(MenuEntity menuEntity) {
        return menuMapper.updateByPrimaryKeySelective(menuEntity);
    }

    /**
     * 删除菜单根据id （后端---菜单管理）
     * gpd 2022-07-18 09:56
     * @param id
     * @return
     */
    @Override
    public Integer deleteMenu(Integer id) {
        MenuEntity menuEntity = new MenuEntity();
        menuEntity.setId(id);
        menuEntity.setDelFlag(1);
        return menuMapper.updateByPrimaryKeySelective(menuEntity);
    }

    /**
     * 遍历父级id下的子级菜单
     * 返回menuEntity
     * @param menuEntity
     * @param menuEntityList
     * @return
     */
    private MenuEntity getChild(MenuEntity menuEntity, List<MenuEntity> menuEntityList) {
        List<MenuEntity> newMenuTree = new ArrayList<>();
        for(MenuEntity childType :menuEntityList){
            if(menuEntity.getId()==childType.getParentId()){
                newMenuTree.add(getChild(childType,menuEntityList));
            }
        }
        menuEntity.setChilds(newMenuTree);
        return menuEntity;
    }
}
